Ext.ns('Ufida', 'Ufida.grid', 'Ufida.widgets')

Ufida.Page = function() {
}
Ufida.Page.prototype = {
	minHeight : '',
	create_widgets : Ext.emptyFn,
	layout_widgets : Ext.emptyFn,
	load_datas : Ext.emptyFn,
	// Call each widget's bind_events method
	bind_events : function() {
		Ext.each(this.widgets, function(widget) {
					if (widget.bind_events) {
						widget.bind_events()
					}
				});
	},
	add_helps : Ext.emptyFn,
	post_created : Ext.emptyFn,
	layout : function(obj) {
		this.widgets = [];
		var def = this._layout(obj, 0);
		if( def.constructor.xtype != 'panel' ){
			def = new Ext.Panel(def);
		}
		// 自适应浏览器窗口大小
		var page = this ;
		var pageWidth , pageHeight ;// main Panel 的高宽 
		syncPageSize();// 调整main面板自适应高宽
		var main  = new Ext.Panel({
			border:false,
			layout:'fit',
			width : pageWidth ,
			height : pageHeight,
			autoWidth : true,		// autoWidth设为true，避免IE、FF中getViewportWidth()的差异
			renderTo : Ext.getBody(),
			items : [def],
			footerCfg: {
//		        html: "<div align='center' style='color:gray;padding:5px;text-align:center;overflow:hidden'>用友软件股份有限公司版权所有  京ICP备06020769号 </div>"
		    }
		});
		// 监听窗口大小改变事件,在窗口大小改变后重新设置主面板的尺寸 自适应浏览器窗口大小
		Ext.EventManager.onWindowResize(handleResize, main);
		function handleResize(w, h){
			syncPageSize();
			main.setSize(pageWidth,pageHeight);
			main.doLayout(false)
	    }
	    // 自适应浏览器窗口，设置main Panel的高宽  
	    function syncPageSize(){
	    	// 网页可见区域高宽
	    	pageWidth = Ext.lib.Dom.getViewportWidth() ;
			pageHeight = Ext.lib.Dom.getViewportHeight() ;
	    	// 自适应浏览器窗口大小 最小尺寸
			if( page.minHeight || page.minWidth ){
				var minHeight = page.minHeight || 605 ;
				var minWidth = page.minWidth || 905 ;
				if( pageWidth < minWidth ){
					pageWidth = minWidth ;
				}
				if( pageHeight < minHeight ){
					pageHeight = minHeight ;
				}
			}
	    }
	    handleResize();
//		new Ext.Viewport(def)
	},
	_layout : function(obj, depth) {
		var items = [];
		for (var key in obj) {
			var value = obj[key];
			var container = false;
			if (!value.id) {
				value = this._layout(value, depth + 1);
				container = true
			}
			Ext.apply(value, {
						region : key
					})

			if (!value.height && key != 'center') {
				if (key == 'east') {
					Ext.apply(value, {
								width : 370,
								height : 185
							})
				} else {
					Ext.apply(value, {
								width : 200,
								height : 185
								
							})
				}
				// 自定义高宽
				if( value.customHeight ){// 设置自定义高度
					Ext.apply( value,{
						height	: value.customHeight
					})
				}
				if( value.customWidth ){// 设置自定义宽度
					Ext.apply( value,{
						width	: value.customWidth
					})
				}

			}

			if (!container) {
				Ext.apply(value.initialConfig, {
							margins : '5 5 0 5'
						});
				// Record All the Widgets
				this.widgets.push(value);
			} else {
				Ext.apply(value, {
							border : false
						})
			}
			items.push(value);
		}
		var def = {
			layout : 'border',
			border : false,
			frame : false,
			autoScroll : false,
			layoutConfig : {
				split : true
			},
			items : items
		}
		if (depth != 0) {
			return new Ext.Panel(def)
		} else {
			return def
		}
	}
}

Ufida.render = function(Page) {
	Ext.onReady(function() {
		var page = new Page();
		page.create_widgets();
		page.layout_widgets();
		page.bind_events();
		page.load_datas();
		page.add_helps();
		page.post_created();
	});
};

/**
 * If not specified scope, it will be assigned to the event source.
 */
Ufida.widgets.bind = function(target_id, event_name, callback, scope) {
	if (!scope) {
		scope = Ext.getCmp(target_id)
	}
	Ext.getCmp(target_id).on(event_name, callback, scope)
};

Ufida.grid.Link = function(config) {
	Ufida.grid.Link.superclass.constructor.call(this, {
		id : Ext.id()
	});
	Ext.apply(this, config);

	this.renderer = this.renderer.createDelegate(this);

	Ext.each(this.ops, function(op) {
		this.addEvents("click_" + op['key']);
	}, this)
};

Ext.extend(Ufida.grid.Link, Ext.util.Observable, {
	init : function(grid) {
		this.grid = grid;
		this.grid.on('render', function() {
			var view = this.grid.getView();
			view.mainBody.on('mousedown', this.onMouseDown, this);
		}, this);
	},

	onMouseDown : function(e, t) {
		if (t.className) {
			Ext.each(this.ops, function(op) {
				if (t.className.indexOf(op['key'] + '-' + this.id) != -1) {
					e.stopEvent();
					var index = this.grid.getView().findRowIndex(t);
										this.grid.getSelectionModel().selectRow(index);
					this.fireEvent("click_" + op['key'], index, this.grid.store
							.getAt(index))
					return false;
				}
			}, this)
		}
	},

	renderer : function(value, cellmeta, r, rowIndex, columnIndex, store) {
//		p.css += ' ufida-link';
		var ret = []
		Ext.each(this.ops, function(op) {
			if (!('isRender' in op)
					|| op['isRender'](value, cellmeta, r, rowIndex,
							columnIndex, store)) {
				ret.push(this.render_link(op))
			}
		}, this)

		return ret.join("  |  ");
	},
	render_link : function(op) {
		return '<span class="ufida-comp-link ' + op['key'] + '-' + this.id
				+ '" style="cursor:pointer">' + op['value'] + '</span>';
	}
});

Ufida.widgets.Link = function(config) {
	Ufida.widgets.Link.superclass.constructor.call(this, {
				id : Ext.id()
			});
	Ext.apply(this, config);
};

Ext.extend(Ufida.widgets.Link, Ext.Component, {
	initComponent : function() {
		Ufida.widgets.Link.superclass.initComponent.call(this);
		this.addEvents('click');
	},
	onRender : function(ct, position) {
		if (!this.template) {
			if (!Ufida.widgets.Link.template)
				Ufida.widgets.Link.template = new Ext.Template('<br/><p><span style="cursor:pointer"><img src="{0}" widht="16" height="16" align="absmiddle" style="margin-right:5px;" />{1}</span></p>');
			this.template = Ufida.widgets.Link.template
		}
		this.el = this.template.append(ct, [this.img, this.value], true)
		this.el.on('click', function(e) {
					this.fireEvent('click', this)
					if (this.handler) {
						this.handler.call(this.scope || this, this);

					}
				}, this)
	}
});

/**
 * class Ufida.grid.LinkExt
 * @param {} config
 */
Ufida.grid.LinkExt = function( config ){
	Ufida.grid.LinkExt.superclass.constructor.call(this, {
		id : Ext.id()
	});
	Ext.apply(this, config);
	this.renderer = this.renderer.createDelegate(this);
	Ext.each(this.ops, function(op) {
			if( op ){
				this.addEvents("click_" + op['key']);
				this.on("click_" + op['key'],op['handler']||Ext.emptyFn,op['scope']||this);
			}
	}, this);
};

Ext.extend(Ufida.grid.LinkExt, Ufida.grid.Link, {
	render_link : function(op) {
		return '<a><span class="ufida-comp-link ' + op['key'] + '-' + this.id
				+ '" style="cursor:pointer">' + op['value'] + '</span></a>';
	}
});

//列表链接
Ufida.grid.handleLink = function(objs,conf){
	var link = new Ufida.grid.LinkExt({
		align:'center',
		header : "操作",
		width : 100,
		ops : objs
	});
	if( conf ){
		Ext.apply( link , conf );
	}
	return link ;
};